package com.shigi.knapsack.solvers.ga;

import com.shigi.knapsack.KnapsackItem;

/**
 * Created by Miroslav Šiagi on 01/12/14.
 */
public class KnapsackPopulation {
    private int idOfPopulation;
    private int numberOfPopulation;
    private int correctSolution;
    private int capacity;

    private KnapsackIndividual[] knapsackIndividuals;

    public KnapsackPopulation(int idOfPopulation, int numberOfPopulation, int numberOfIndividuals, int correctSolution, int capacity) {
        this.idOfPopulation = idOfPopulation;
        this.numberOfPopulation = numberOfPopulation;
        this.knapsackIndividuals = new KnapsackIndividual[numberOfIndividuals];
        this.correctSolution = correctSolution;
        this.capacity = capacity;
    }

    public KnapsackPopulation(KnapsackPopulation anotherKnapsackPopulation) {
        this.idOfPopulation = anotherKnapsackPopulation.getIdOfPopulation();
        this.numberOfPopulation = anotherKnapsackPopulation.getNumberOfPopulation();
        this.correctSolution = anotherKnapsackPopulation.getCorrectSolution();
        this.capacity = anotherKnapsackPopulation.getCapacity();

        copyIndividuals(anotherKnapsackPopulation);

    }

    private void copyIndividuals(KnapsackPopulation anotherKnapsackPopulation) {
        KnapsackIndividual[] anotherKnapsackIndividuals = anotherKnapsackPopulation.getKnapsackIndividuals();
        int numOfAnKnapIndividuals = anotherKnapsackIndividuals.length;
        this.knapsackIndividuals = new KnapsackIndividual[numOfAnKnapIndividuals];

        for (int i = 0; i < numOfAnKnapIndividuals; i++) {
            KnapsackItem[] anoterKnapsackItems = anotherKnapsackIndividuals[i].getKnapsackItems();
            int numOfItems = anoterKnapsackItems.length;
            this.knapsackIndividuals[i] = new KnapsackIndividual(numOfItems);

            KnapsackItem[] newItems = new KnapsackItem[numOfItems];
            for (int j = 0; j < numOfItems; j++) {
                KnapsackItem knapsackItem = new KnapsackItem(anoterKnapsackItems[j].getId());
                knapsackItem.setUsed(false);
                knapsackItem.setWeight(anoterKnapsackItems[j].getWeight());
                knapsackItem.setValue(anoterKnapsackItems[j].getValue());
                newItems[j] = knapsackItem;
            }
            this.knapsackIndividuals[i].setKnapsackItems(newItems);
        }
    }

    public KnapsackIndividual[] getKnapsackIndividuals() {
        return knapsackIndividuals;
    }

    public void setKnapsackIndividuals(KnapsackIndividual[] knapsackIndividuals) {
        this.knapsackIndividuals = knapsackIndividuals;
    }

    public int getNumberOfPopulation() {
        return numberOfPopulation;
    }

    public void setNumberOfPopulation(int numberOfPopulation) {
        this.numberOfPopulation = numberOfPopulation;
    }

    public int getCorrectSolution() {
        return correctSolution;
    }

    public void setCorrectSolution(int correctSolution) {
        this.correctSolution = correctSolution;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public int getIdOfPopulation() {
        return idOfPopulation;
    }

    public void setIdOfPopulation(int idOfPopulation) {
        this.idOfPopulation = idOfPopulation;
    }
}
